package com.tju.backend.resources.controller.open;

import com.tju.backend.dao.ware.StereoscopicWarehouseMapper;
import com.tju.backend.entity.OutWarehouseLog;
import com.tju.backend.entity.TProduct;
import com.tju.backend.entity.TStereoscopicWarehouse;
import com.tju.backend.plugin.common.GlobalCall;
import com.tju.backend.plugin.common.Push;
import com.tju.backend.service.business.IStereoscopicWarehouseService;
import com.tju.backend.service.business.IWarehousingService;
import com.tju.backend.utils.result.Result;
import com.tju.backend.utils.result.ResultEnum;
import com.tju.backend.utils.result.ResultUtil;
import com.tju.backend.utils.tool.ClientParameters;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.tju.backend.plugin.common.GlobalCall.*;

/**
 * @USER: CLS
 */
@RestController
@Slf4j
public class HomeController {
    private final String prefix = "home";

    @Autowired
    private StereoscopicWarehouseMapper stereoscopicWarehouseMapper;
    @Autowired
    private IStereoscopicWarehouseService stereoscopicWarehouseService;

    @Autowired
    private IWarehousingService warehousingService;

    /**
     * 查询订单列表
     */
    @ResponseBody
    @PostMapping(prefix + "/kuWei")
    public Result<Object> kuWei() {
        //总数
        int total = stereoscopicWarehouseMapper.getTotal();
        //已用
        int num = stereoscopicWarehouseMapper.getUsedNum();
        //剩余
        int surplus = total - num;
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("num", num);
        map.put("surplus", surplus);
        return ResultUtil.success(ResultEnum.SUCCESS, map);
    }

    /**
     * 查询入仓日志
     */
    @ResponseBody
    @PostMapping(prefix + "/getWarehousing")
    public Result<Object> getWarehousing() {
        return warehousingService.listWarehousing();
    }

    /**
     * 查询出仓日志
     */
    @ResponseBody
    @PostMapping(prefix + "/getOutWarehouseLog")
    public Result<Object> getOutWarehouseLog() {
        return stereoscopicWarehouseService.getOutWarehouseLog();
    }

    /**
     * 查询订单列表
     */
    @ResponseBody
    @PostMapping(prefix + "/getOrder")
    public Result<Object> getOrder(@RequestBody ClientParameters parameters) {
        return stereoscopicWarehouseService.getOrder(parameters);
    }

    /**
     * 出仓
     */
    @PostMapping(prefix + "/outWarehouse")
    public Result<Object> outWarehouse(@RequestBody ClientParameters parameters) {
        return stereoscopicWarehouseService.outWarehouse(Integer.valueOf(parameters.getUgc()));
    }

    /**
     * 发送给plc
     */
    @PostMapping(prefix + "/PushPlc")
    public void PushPlc(String msg) {
        if (msg.equals("A05") || msg.equals("A04")) {
            if (msg.equals("A05")) {
                TStereoscopicWarehouse ws = sw.selectAllstatus(3);
                if (ws != null) {
                    sw.updateStatus(0, ws.getId());
                }
                if (redisService.hasKey("liku") && result) {
                    String likuValue = redisService.lGetIndex("liku", 0).toString();
                    TStereoscopicWarehouse tsw = null;
                    if (likuValue != null && likuValue != "") {
                        redisService.lRemove("liku", 0, likuValue);
                        tsw = sw.selectStereoscopicWarehouseById(Integer.valueOf(likuValue));
                    }
                    if (tsw != null) {
                        //修改立体库位状态
                        int rows = sw.updateStatusByBarcode(tsw.getBoxBarcode(), new Date());
                        TStereoscopicWarehouse outSw = sw.selectOne(tsw.getBoxBarcode());
                        //todo: 进行PLC发送出货
                        Push.tcpPush(GlobalCall.str(outSw));
                        //出仓
                        Push.push(ResultUtil.success(ResultEnum.OUTWAREHOUSE, GlobalCall.str(outSw)));
                        //封装参数
                        outsw.insertOutWarehouseLog(new OutWarehouseLog(tsw, rows));
                    }
                    List<String> list = new ArrayList<>();
                    long likuSize = redisService.lGetListSize("liku");
                    if (likuSize != 0) {
                        for (int likuIndex = 0; likuIndex < likuSize; likuIndex++) {
                            String liku = redisService.lGetIndex("liku", likuIndex).toString();
                            list.add(liku);
                        }
                    }
                    log.info("缓存数据" + ResultUtil.success(ResultEnum.OUTWAREHOUSETwo, list) + "");
                    //缓存的数据
                    Push.push(ResultUtil.success(ResultEnum.OUTWAREHOUSETwo, list));
                }
            }
            Push.push(ResultUtil.success(ResultEnum.ROLLER, msg));
        } else {
            Push.msgPush(msg);
        }

    }

    @PostMapping(prefix + "/list")
    public void list(String name) {
        System.err.println(ResultUtil.success(ResultEnum.STEREOSCOPIC_WAREHOUSESELECT, sw.selectAllTaskOccupy(new TProduct())));
    }
}
